package jwanalytics.agentbasedsimulations;

import sim.engine.SimState;
import sim.engine.Steppable;
import sim.field.grid.IntGrid2D;

/** 
 * Here you program how you are going to animate/automate the cells which are in
 * this simulation occupied by agents which are web site visitors (WebSiteVisitorAgent).
 * You can decide to fire all agents together by stepping them all.  You can decide to 
 * call a random number of agents/visitors step method once you receive one step call
 * here (these are simulation steps, but not agent steps), agents can stay without 
 * receiving propagations of these steps called on this class.
 * @author aacesta
 */
public class WebSiteVisitorCellAutomata implements Steppable {
	
	private static final long serialVersionUID = 1L;
	
	/** Local copy of the Tourism003 grid. */
	public IntGrid2D tempGrid = new IntGrid2D(0,0);	
	
	@Override
	public void step(SimState state) {
    	BuiltInSimulation builtInSimulation = (BuiltInSimulation)state;
    	tempGrid.setTo(builtInSimulation.grid);   // first copy the grid into tempGrid
    	
    	//For each cell compute the state.
    	int width = tempGrid.getWidth();
    	int height = tempGrid.getHeight();
    	
    	for(int x=0;x<width;x++)
    		for(int y=0;y<height;y++) {
    			//Display current state.
    			WebSiteVisitorAgent agent = ((WebSiteVisitorAgent)builtInSimulation.agents[x][y]);
    			builtInSimulation.grid.field[x][y] = agent.getPageCode().ordinal(); //which is zero or one now.
    			//
    			System.out.println("Agent state:"+tempGrid.field[x][y]);
    			//Advance every agent one step for next display.
    			((WebSiteVisitorAgent)builtInSimulation.agents[x][y]).step(builtInSimulation.schedule.getSteps());    			
    		}		
	}
	
	
	
}
